<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    ul,
    ol {
        list-style: none;
    }

    .box {
        width: 300px;
        height: 250px;
        border: 2px solid hotpink;
        margin: 200px auto 0;
    }

    ul {
        overflow: hidden;
    }

    ul li {
        background: orange;
        width: 98px;
        height: 50px;
        border-left: 1px solid black;
        border-right: 1px solid black;
        float: left;
        font: 20px/50px "";
        text-align: center;
    }

    ul li.active {
        background-color: orangered;
    }

    ol li {
        background: skyblue;
        width: 300px;
        height: 200px;
        font: 50px/200px "";
        text-align: center;
        display: none;
    }

    ol li.active {
        display: block;
    }
</style>

<body>
    <div class="box">
        <ul>
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
        </ul>
        <ol>
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
        </ol>
    </div>
</body>
<script>
    //先获取元素
    var btns = document.querySelectorAll("ul li")
    var tabs = document.querySelectorAll("ol li")
    //循环,给每一个按钮添加点击事件
    btns.forEach(function (item, index) {
        item.onclick = function () {
            //当按钮被点击时执行以下操作
            //1.给所以按钮和选项卡清除active类名
            btns.forEach(function (item,index) {//这里的item和index都是这个函数里的局部变量
                item.className = ""
                tabs[index].className = ""
            })
            //2.给被点击的按钮添加active类名
            item.className = "active"//这里的用this和item都一样
            //3.给对应的选项卡添加active类名
            tabs[index].className = "active"
        }
    })
</script>

</html>